/**
 *  题目一：通讯录管理系统的设计与实现 
 设计要求：系统包括通讯者结点信息的插入、查询、删除、
 更新以及通讯录信息的输出等功能。 
  菜单内容： 
  1. 通讯录链表的建立 
  2. 通讯者信息的插入 
  3. 通讯者信息的查询 
  4. 通讯者信息的修改 
  5. 通讯者信息的删除 
  6. 通讯录链表的输出 
  7. 退出管理系统 
  请选择：1 – 7：
 */

#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>
#include <cstring>
using namespace std;
const int MAXSIZE = 1000;

typedef int ID;

void menu();

class Contact {
	private:
		struct man {
			string name;
			string phoneNumber;
			string address;
			string birthday;
		};
		string name, number;

	public:
		int nCount;
		enum Stat {SUCCESS, FAILURE, FULL, EMPTY};
		man *person = new man[MAXSIZE];
		// 构造函数
		Contact() {
			nCount = 0;
			ifstream file("Contact.txt");
			while(file) {
				file >> person[nCount].name >> person[nCount].phoneNumber >> person[nCount].address >> person[nCount].birthday;
				++nCount;
			}
			cout << "Contact has " << nCount-1 << " member." << endl;
		}

		// 析构函数
		~Contact() {
			delete []person;
		}

		// 拷贝构造函数
		//Contact(const Contact &pt) {
		//	
		//}

		//int cmp(struct man a, struct man b) {
		//	return a.name < b.name;
		//}

		Stat Insert() {
			if(nCount >= MAXSIZE) {
				cout << "Sorry, Contact is Full." << endl;
				exit(-1);
			}
			
			cout << "Name: ";
			cin >> person[nCount].name;
			cout << "PhoneNumber: ";
			cin >> person[nCount].phoneNumber;
			cout << "Address: ";
			cin >> person[nCount].address;
			cout << "Birthday: ";
			cin >> person[nCount].birthday;

			nCount++;

			//sort(person, person+nCount, cmp);

			return SUCCESS;
		}

		Stat IsEmpty() {
			if(nCount <= 0) {
				cout << "Sorry, Contact has no member." << endl;
				return EMPTY;
			}
			return FULL;
		}

		ID Search_By_Name(string name) {
			for(int i=0; i!=nCount; i++) {
				if(name == person[i].name) {
					cout << "Find." << endl;
					cout << "Name: " << person[i].name << endl;
					cout << "PhoneNumber: " << person[i].phoneNumber << endl;
					cout << "Address: " << person[i].address << endl;
					cout << "Birthday: " << person[i].birthday << endl;
					return i;
				}
			}

			cout << "Contact has no member named " << name << " ." << endl;

			return -1;
		}

		ID Search_By_Phone(string phoneNumber) {
			for(int i=0; i!=nCount; i++) {
				if(phoneNumber == person[i].phoneNumber) {
					cout << "Find." << endl;
					cout << "Name: " << person[i].name << endl;
					cout << "PhoneNumber: " << person[i].phoneNumber << endl;
					cout << "Address: " << person[i].address << endl;
					cout << "Birthday: " << person[i].birthday << endl;
					return i;
				}
			}

			cout << "Contact has no member whose phone number is " << phoneNumber << "." << endl;

			return -1;
		}

		Stat Search() {
			if(IsEmpty() == EMPTY)
				return FAILURE;
			
			cout << "1、By Name" << endl;
			cout << "2、By PhoneNumber" << endl;
			cout << "Please Choose: " << endl;

			string choice;
			cin >> choice;
			while(choice[0] != '1' && choice[0] != '2') {
				cout << "Input Error. Try again." << endl;
				cin >> choice;
			}

			switch(choice[0]) {
				case '1':
					cout << "Enter Name: ";
					cin >> name;
					Search_By_Name(name);
					break;
				case '2':
					cout << "Enter Phone Number: ";
					cin >> number;
					Search_By_Phone(number);
					break;
				default:
					break;
			}

			return SUCCESS;
		}

		Stat Modify() {
			if(IsEmpty() == EMPTY)
				return FAILURE;

			cout << "1、By Name" << endl;
			cout << "2、By PhoneNumber" << endl;
			cout << "Please Choose: " << endl;

			string choice;
			cin >> choice;
			while(choice[0] != '1' && choice[0] !='2') {
				cout << "Input Error. Try again." << endl;
				cin >> choice;
			}

			ID one;
			switch(choice[0]) {
				case '1':
					cout << "Enter Name: ";
					cin >> name;
					one = Search_By_Name(name);
					if(one != -1) {
						cout << "Name: ";
						cin >> person[one].name;
						cout << "Phone Number: ";
						cin >> person[one].phoneNumber;
						cout << "Address: ";
						cin >> person[one].address;
						cout << "Birthday: ";
						cin >> person[one].birthday;

						cout << "Update Successfully." << endl;
					}
					break;
				case '2':
					cout << "Enter Phone Number: ";
					cin >> number;
					one = Search_By_Phone(number);
					if(one != -1) {
						cout << "Name: ";
						cin >> person[one].name;
						cout << "Phone Number: ";
						cin >> person[one].phoneNumber;
						cout << "Address: ";
						cin >> person[one].address;
						cout << "Birthday: ";
						cin >> person[one].birthday;
						
						cout << "Update Successfully." << endl;
					}
					break;
				default:
					break;
			}

			return SUCCESS;
		}

		Stat Delete() {
			if(IsEmpty() == EMPTY)
				return FAILURE;

			cout << "1、By Name" << endl;
			cout << "2、By PhoneNumber" << endl;
			cout << "Please Choose: " << endl;

			string choice;
			cin >> choice;
			while(choice[0] != '1' && choice[0] !='2') {
				cout << "Input Error. Try again." << endl;
				cin >> choice;
			}
	
			ID one;
			switch(choice[0]) {
				case '1':
					cout << "Enter Name: ";
					cin >> name;
					one = Search_By_Name(name);
					if(one != -1) {
						for(int i=one; i!=nCount; i++) {
							person[i] = person[i+1];
						}
						--nCount;
					}
					break;
				case '2':
					cout << "Enter Phone Number: ";
					cin >> number;
					one = Search_By_Phone(number);
					if(one != -1) {
						for(int i=one; i!=nCount; i++) {
							person[i] = person[i+1];
						}
						--nCount;
					}
					break;
				default:
					break;
			}
			
			return SUCCESS;
		}

		Stat Show() {
			if(IsEmpty() == EMPTY)
				return FAILURE;

			cout << "Contact has " << nCount << " members." << endl;

			for(int i=0; i!=nCount; i++) {
					cout << person[i].name << " ";
					cout << person[i].phoneNumber << " ";
					cout << person[i].address << " ";
					cout << person[i].birthday << endl;
			}

			return SUCCESS;
		}

		Stat StoreAsFile() {
			if(IsEmpty() == EMPTY)
				return FAILURE;
			ofstream file("Contact.txt");
			for(int i=0; i!=nCount; i++) {
				file << person[i].name << " ";
				file << person[i].phoneNumber << " ";
				file << person[i].address << " ";
				file << person[i].birthday << endl;
			}
			
			return SUCCESS;
		}




};

int main(void) {
	int choice;
	Contact Host;
	while(1) {
		menu();
		cin >> choice; 
		
		switch(choice) {
			case 1:
				break;
			case 2:
				Host.Insert();
				break;
			case 3:
				Host.Search();
				break;
			case 4:
				Host.Modify();
				break;
			case 5:
				Host.Delete();
				break;
			case 6:
				Host.Show();
				break;
			case 7:
				Host.StoreAsFile();
				exit(1);
				break;
			default:
				cout << "Input Error.Try again." << endl << endl;
				// system("pause");
				// system("clear");
				break;
		}
		cout << "按任意键继续。。。" << endl;
		getchar();
		getchar();
	}

 	
	return 0;
 }

void menu() {
	cout << "***********************" << endl;
	cout << "* 菜单内容：          *" << endl;
	cout << "* 1. 通讯录链表的建立 *" << endl;
	cout << "* 2. 通讯者信息的插入 *" << endl;
	cout << "* 3. 通讯者信息的查询 *" << endl;
	cout << "* 4. 通讯者信息的修改 *" << endl;
	cout << "* 5. 通讯者信息的删除 *" << endl;
	cout << "* 6. 通讯录链表的输出 *" << endl;
	cout << "* 7. 退出管理系统     *" << endl;
	cout << "***********************" << endl;
	cout << "*请选择：1 – 7: " << endl;
}
